package com.ylfstu.map_;

import java.util.*;

@SuppressWarnings({"all"})
public class MapFor {
    public static void main(String[] args) {

        Map map = new HashMap();
        map.put("邓超", "孙俪");
        map.put("王宝强", "马蓉");
        map.put("宋喆", "马蓉");
        map.put("刘令博", null);
        map.put(null, "刘亦菲");
        map.put("鹿晗", "关晓彤");

        //第一组: 先取出所有的 Key , 通过 Key 取出对应的 Value
        Set keySet = map.keySet();
        //(1)增强for
        System.out.println("-----第一种方式-----");
        for (Object key : keySet) {
            System.out.println(key + "-" + map.get(key));
        }
        //(2)迭代器
        System.out.println("-----第二种方式-----");
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + "-" + map.get(key));
        }

        //第二组：把所有的 values 取出
        Collection values = map.values();
        //(1)增强for
        System.out.println("-----取出values增强for-----");
        for (Object value : values) {
            System.out.println(value);
        }
        //(2)迭代器
        System.out.println("-----取出values迭代器-----");
        Iterator iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object value = iterator1.next();
            System.out.println(value);
        }

        //第三组：通过entrySet 来获取k-v
        Set entrySet = map.entrySet();//EntrySet<Map.Entry<k,v>>
        //(1)增强for
        System.out.println("-----使用entrySet 增强for");
        for (Object entry : entrySet) {
            //将entry 转成 Map.entry
            Map.Entry entry1 = (Map.Entry) entry;
            System.out.println(entry1.getKey() + "-" + entry1.getValue());
            System.out.println();
        }
        //(2)迭代器
        System.out.println("-----使用entrySet 迭代器-----");
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Object next = iterator2.next();
            Map.Entry next1 = (Map.Entry) next;
            System.out.println(next1.getKey() + "-" + next1.getValue());
        }

    }
}








